<?php
declare (strict_types = 1);

namespace app\home\controller;

use app\home\model\PygUser;
use app\home\userBusiness\UserBusiness;
use think\Exception;
use think\facade\Cache;
use think\facade\Session;
use think\facade\View;
use think\Request;
use think\response\Redirect;

class Login
{
    /**
     * 登录页面方法
     * @return \think\response\View
     */
    public function login(){
        Session::delete('name');
        View::config(['layout_on'=>  false]);
        return view('login');
    }


    /**
     * 注册页面方法
     * @return \think\response\View
     */
    public function register(){
        View::config(['layout_on'=>  false]);
        return view();
    }

    /**
     * 第三方登录
     * @return Redirect
     */
    public function qqcallback(){
        require './qq/API/qqConnectAPI.php';
        $qc = new \QC();
        $token = $qc->get_access_token();
        $get_id = $qc->get_openid();
        $qc = new \QC($token,$get_id);
        $user = $qc->get_user_info();
        UserBusiness::qq($user,$get_id);
        return \redirect('/');
    }

    /**
     * 发送验证码
     * @return \think\response\Json
     */
    public function captcha(){
        try {
            $phone = \request()->post('phone');
            //验证手机号
            if (strlen($phone) != 11){
                abort(2001,'手机号格式错误');
            }
            $data = UserBusiness::sendCaptcha($phone);
            return json(['code'=>200,'msg'=>'ok','data'=>[$data]]);
        }catch (\Exception $exception){
            return json(['code'=>$exception->getCode(),'msg'=>$exception->getMessage(),'data'=>[]]);
        }
    }


    /**
     *完成注册
     */
    public function doRegister(){
        try {
            $data = \request()->all();
            $phone = \request()->post('phone');
            $code = \request()->post('code');
            $cache_code = Cache::get('reg_a'.$phone);
            $password = password($data['password']);
            if (empty($cache_code)){
                abort(2001,'验证码错误');
            }
            if ($code != $cache_code){
                abort(2002,'验证码不正确');
            }
            Cache::delete('reg_a'.$phone);
            $phone = substr_replace($phone,'****',3,4);
            PygUser::create([
                'phone' => $phone,
                'password' => $password,
                'username' => $phone,
                'nickname' => $phone
            ]);
            return json(['code'=>200,'msg'=>'注册成功','data'=>[]]);
        }catch (\Exception $exception){
            return json(['code'=>$exception->getCode(),'msg'=>$exception->getMessage(),'data'=>[]]);
        }
    }

    /**
     * 登录的方法
     */
    public function add(){

        try {
            $phone = substr_replace(\request()->post('phone'),'****',3,4);
            $password = \request()->post('password');
            $res = PygUser::where('phone',$phone)->find();
            if (!$res){
                return json(['code'=>2001,'msg'=>'账号不存在']);
            }elseif (password($password) != $res['password']){
                return json(['code'=>2002,'msg'=>'密码错误']);
            }else{
                $data = substr_replace($phone,'****',3,4);
               Session::set('name',['data'=>$data,'id'=>$res->id]);
                return json(['code'=>200,'msg'=>'登录成功','data'=>[]]);
            }
        }catch (\Exception $exception){
            return json(['code'=>$exception->getCode(),'msg'=>$exception->getMessage(),'data'=>[]]);
        }

    }

}
